<?php

App::import("Vendor", "Smsgh/Api");

class SmsComponent extends Component {
    
    private $api = null;
    private $account = null;
    
    public function initialize(\Controller $controller) {
        parent::initialize($controller);
        
        $auth = new BasicAuth( Configure::read('smsgh.clientId'), Configure::read('smsgh.secret'));
        $host = new ApiHost($auth);
        $this->api = new MessagingApi($host);
        $this->account = new AccountApi($host);
    }
    
    public function send($to = null, $text = null, $sender_id = 'Memberz') {
     
        if( $to == null ) {
            $to = "+233277593003";
        }
        
        if( $text == null ) {
            $text = "Hello there. This is a test message from memberz.org";
        }
        
        try {
            $message = new Message();
            $message->setFrom($sender_id);
            $message->setTo($to);
            $message->setContent($text);
            $message->setRegisteredDelivery(true);
            
            $messageResponse = $this->api->sendMessage($message);

            if ($messageResponse instanceof MessageResponse) {
                return array('status' => $messageResponse->getStatus());
            } elseif ($messageResponse instanceof HttpResponse) {
                return array('status' => $messageResponse->getStatus()); 
            }
            
        } catch (Exception $ex) {
            return array('error' => $ex->getTraceAsString());
        }
        
    }
    
    public function schedule($to = null, $text = null, $time = null) {
        
        if( $to == null ) {
            $to = "+233277593003";
        }
        
        if( $text == null ) {
            $text = "Hello there. This is a test message from memberz.org";
        }
        
        if( $time == null ) {
            $time = date("Y-m-d H:i:s");
        }
        
        try {
            $message = new Message();
            $message->setFrom('Memberz');
            $message->setTo($to);
            $message->setContent($text);
            $message->setRegisteredDelivery(true);
            $message->setTime($time);
            $messageResponse = $this->api->sendMessage($message);

            if ($messageResponse instanceof MessageResponse) {
                return array('status' => $messageResponse->getStatus());
            } elseif ($messageResponse instanceof HttpResponse) {
                return array('status' => $messageResponse->getStatus()); 
            }
            
        } catch (Exception $ex) {
            return array('error' => $ex->getTraceAsString());
        }
        
    }
    
    /*public function reschedule($messageId, $new_time) {
        
        $response = $this->api->getMessages()->reschedule($messageId, strtotime($new_time));
        
        return compact('response');
    }*/
    
    /**
     * Retrieve scheduled messageIds from database
     * 
     * @param type $messageId
     * @return type
     */
    /*public function cancelScheduled($messageId) {
        
        $response = $this->api->getMessages()->cancel($messageId);
        
        return compact('response');
    }
    
    public function getSent() {
        $apiList = $this->api->getMessages()->get(-1, -1, 100, 2, false, null);
        $count    = $apiList->getCount();
        $messages = $apiList->getItems();
        
        return array('count' => $count, 'messages' => $messages);
    }*/
    
    public function profile() {
        try {
            $profile = $this->account->getProfile();
            
            return compact('profile');
        } catch (Exception $ex) {
            return array('error' => $ex->getTraceAsString());
        }
    }
}
